package com.sl.au.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.swing.SortOrder;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.sl.au.entity.EventRegister;
import com.sl.au.entity.EventReport;
import com.sl.au.entity.EventToEvaluateUser;
import com.sl.au.entity.ViewForUserScoreAnalysis;
import com.sl.au.service.EventRegisterService;
import com.sl.au.service.EventReportService;
import com.sl.au.service.EventToEvaluateUserService;
import com.sl.au.service.ViewForUserScoreAnalysisService;
import com.sl.utils.ExdeUtil;
import com.sl.utils.MsgHelper;

@RestController
@RequestMapping(value = "/ui/EventAnalysis")
public class EventAnalysisController extends BaseController {

	@Autowired
	ViewForUserScoreAnalysisService viewForUserScoreAnalysisService;
	@Autowired
	EventToEvaluateUserService eventToEvaluateUserService;
	
	@Autowired
	EventReportService eventReportService;
	
	@Autowired
	EventRegisterService eventRegisterService;
	

	@RequestMapping(method = RequestMethod.GET)
	public ModelAndView index() {
		ModelAndView modelAndView = this.createLayoutView("analysis/eventanalysisindex");
		return modelAndView;
	}
	
	@RequestMapping(value = "getEventPage", method = RequestMethod.POST)
	public Map<String, Object> getEventPage(HttpServletRequest request) {
		Map<String, Object> map = new HashMap<String, Object>();
		int start = Integer.parseInt(request.getParameter("start"));
		int length = Integer.parseInt(request.getParameter("length"));
		String key = request.getParameter("key");
		
		List<EventRegister> pages = null;
		int count = 0;
		String cyc = request.getParameter("cyc");
		String begin = request.getParameter("beginDate");
		String end = request.getParameter("endDate");
		
		Date beginDate = null;
		Date endDate = null;
		if(cyc.equals("week")) {
			beginDate = ExdeUtil.getThisWeekBegin();
			endDate = ExdeUtil.getThisWeekEnd();
		}else if(cyc.equals("month")) {
			beginDate = ExdeUtil.getThisMonthBegin();
			endDate = ExdeUtil.getThisMonthEnd();
		}else if(cyc.equals("year")) {
			beginDate = ExdeUtil.getThisYearBegin();
			endDate = ExdeUtil.getThisYearEnd();
		}else {
			String pattern = "yyyy-MM-dd HH:mm:ss";
			begin += " 00:00:00";
			end += " 23:59:59";
			beginDate = ExdeUtil.str2Date(begin, pattern);
			endDate = ExdeUtil.str2Date(end, pattern);
			
		}
		
		pages = eventRegisterService.getPageByEventDate(start, length, beginDate, endDate, "createTime", Sort.Direction.DESC);
		count = eventRegisterService.getCountByEventDate(beginDate, endDate);

		
		map.put("data", pages);
		map.put("draw", request.getParameter("draw"));
		map.put("recordsFiltered", count);
		map.put("recordsTotal", count);
		return map;
	}
	
	@RequestMapping("/getAnalysisData/")
	public Map<String, Object> getAnalysisData(@RequestBody Map<String,Object> map) {
		
		String cyc = (String) map.get("cyc");
		String begin = (String) map.get("beginDate");
		String end = (String) map.get("endDate");
		
		Date beginDate = null;
		Date endDate = null;
		if(cyc.equals("week")) {
			beginDate = ExdeUtil.getThisWeekBegin();
			endDate = ExdeUtil.getThisWeekEnd();
		}else if(cyc.equals("month")) {
			beginDate = ExdeUtil.getThisMonthBegin();
			endDate = ExdeUtil.getThisMonthEnd();
		}else if(cyc.equals("year")) {
			beginDate = ExdeUtil.getThisYearBegin();
			endDate = ExdeUtil.getThisYearEnd();
		}else {
			String pattern = "yyyy-MM-dd HH:mm:ss";
			begin += " 00:00:00";
			end += " 23:59:59";
			beginDate = ExdeUtil.str2Date(begin, pattern);
			endDate = ExdeUtil.str2Date(end, pattern);
			
		}
		
		Map<String, Object> data  = viewForUserScoreAnalysisService.getEventAnalysisData(beginDate, endDate);
		
		data.put("code", 1);
		return data;
	}

}
